<?php
/**
 * Model for JA News Component
 * @subpackage  janews component
 * @author Joomlart.com
 * @license GNU/GPL
 */
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');
require_once(JPATH_COMPONENT_ADMINISTRATOR.DS.'models'.DS.'frontpage.php');

/**
 * News Model
 *
 * @package    Joomla
 * @subpackage Components
 */
class JanewsModelcommon extends JanewsModelfrontpage
{
  /**
   * Check homepage
   * @return itemid is default home
   */
  function getCurrentHomepage()
  {
    $db = JFactory::getDBO();
    $query = "SELECT *"
      ." FROM #__menu "
      ." WHERE home = 1";
    $db->setQuery( $query );
    $cur= $db->loadObject();
    if ( !$db->query() ) {
			$this->setError($db->getErrorMsg());
			return 0;
		}
    return $cur;
  }
	/**
	 * Check exits janews component menu
	 *
	 * @param int componentid
	 * @return true if the menu exits
	 */
	function checkComponentMenu($id)
	{
	  $db = &JFactory::getDBO();
		$query = "SELECT count(*)"
      ." FROM #__menu"
      ." WHERE componentid=".$id." AND menutype != 'janews' AND published = 1";
		$db->setQuery($query);
		if ( !$db->query() ) {
			$this->setError($db->getErrorMsg());
			return false;
		}
		$num = $db->loadResult();
		$exits = ($num > 0) ? true : false; 
		return $exits;
	}
	
	function setDefaultHomePage($comid)
	{
	  $db = &JFactory::getDBO();
	  //get menu of JA News component for set default homepage
	  $query = "SELECT id,ordering"
      ." FROM #__menu"
      ." WHERE componentid=".$comid." AND menutype != 'janews' AND home = 0 AND published = 1"
      ." ORDER BY ordering, id";
    $db->setQuery($query);
    if ( !$db->query() ) {
			$this->setError($db->getErrorMsg());
			return false;
		}
    $com_menu = $db->loadObject();
    
	  $curHome = $this->getCurrentHomepage();
	  //un set default home page current
	  $query = "UPDATE #__menu"
	   ." SET home = 0"//, ordering = ".$com_menu->ordering
	   ." WHERE id = ".$curHome->id;
	  $db->setQuery($query);
	  if ( !$db->query() ) {
			$this->setError($db->getErrorMsg());
			return false;
		}

    //set default home page
    $query = "UPDATE #__menu"
      ." SET home = 1, menutype = '$curHome->menutype'"
      ." WHERE id = ".$com_menu->id;
    $db->setQuery($query);
    if ( !$db->query() ) {
			$this->setError($db->getErrorMsg());
			return false;
		}
	}
	
	/**
	 * un set defautl homepage for JA News component
	 */
	function unDefautlHomePage($comid)
	{	  
	  $curHome = $this->getCurrentHomepage();
	  $db = &JFactory::getDBO();
	  $query = "SELECT id"
	   ." FROM #__menu"
	   ." WHERE menutype='$curHome->menutype' AND published = 1 AND home = 0"
	   ." ORDER BY ordering";
    $db->setQuery($query);
    $db->query();
    $newHomeId = (int) $db->loadResult();
    if ($newHomeId > 0){
      $query = "UPDATE #__menu"
        ." SET home = 1"
        ." WHERE id = ".$newHomeId;
      $db->setQuery($query);
      $db->query();
 
      $query = "UPDATE #__menu"
  	    ." SET home = 0"
  	    ." WHERE id = ".$curHome->id;
	    $db->setQuery($query);
	    $db->query();
    }
	}
  /**
   * Check home page default for JA News component
   * @param id of JA News component
   */
	function checkComHomePage($id)
	{
    $db = &JFactory::getDBO();
    $query = "SELECT count(*)"
      ." FROM #__menu"
      ." WHERE componentid=".$id." AND menutype != 'janews' AND home = 1";
    $db->setQuery($query);
    if ( !$db->query() ) {
			$this->setError($db->getErrorMsg());
			return false;
		}
		$num = $db->loadResult();
		$exits = ($num > 0) ? true : false; 
		return $exits; 
	}
  
	/**
	 * load data row of #__component by option = 'com_janews'
	 */
	/*function getComponentTable()
	{
	  $tbl =& JTable::getInstance('component');
	  $tbl->loadByOption( 'com_janews' );
	  return $tbl;
	}
	*/	

	/**
	 * Disable or Enable JA News component
	 * @return message about status of update action
	 */
	function updateComponentTable($enable)
	{
	  $component =& JComponentHelper::getComponent('com_janews');
	  $update = $component->enabled != $enable;
	  if ($update) {
	    $db = JFactory::getDBO();
	    $query = "UPDATE #__components as c"
        ." SET enabled =".$enable
        ." WHERE c.option = 'com_janews' AND c.id ='".$component->id."'";
	    $db->setQuery($query);
	    if ( !$db->query() ) {
  			$this->setError($db->getErrorMsg());
  			return false;
		  }
	  }
	  return $update;
	}
	
	function createComponentMenu($sethomepage=false)
	{
  	$db = JFactory::getDBO();
  	$component =& JComponentHelper::getComponent('com_janews');
  	$params =	'show=0\nid=0\nhomepage=\r\nendable=\r\npage_title=\r\nshow_page_title=1\npageclass_sfx=\r\nmenu_image=-1\nsecure=0\n';
  	$status = 'Success';
  	//check menu type current
  	$query = "SELECT menutype"
  	  ." FROM #__menu"
      ." WHERE home = 1"; 
  	$db->setQuery( $query );
  	if ( !$db->query() ) {
  		$this->setError($db->getErrorMsg());
  		$status = 'Failure';
  		return $status;
		}
		else {
		 $menutype = $db->loadResult(); 
		}
		if ($sethomepage){
		  $query = "UPDATE #__menu SET home = 0 WHERE home = 1";
		  $db->setQuery($query);
		  $db->query();
		  if ( !$db->query() ) {
  		$this->setError($db->getErrorMsg());
  		$status = 'Failure';
  		return $status;
		  }
		}
    $query = "INSERT INTO #__menu (menutype,name,alias,link,type,published,parent,componentid,sublevel,ordering,checked_out,checked_out_time,pollid,browserNav,access,utaccess,params,home)
      VALUES ('".$menutype."','JA News','ja-news','index.php?option=com_janews&view=janews','component','1','0','".$component->id."','0','0','0','0000-00-00 00:00:00','0','0','0','0','".$params."','".($sethomepage ? '1' : '0')."')";
    $db->setQuery( $query );
  	if ( !$db->query() ) {
  	  $this->setError($db->getErrorMsg());
  		$status = 'Failure';
  		return $status;
  	}
  	return $status;
  }

  /**
	 * create menu for section
	 */
	function creatMenuSection($sec)
	{
	  $db = JFactory::getDBO();
	  $query = "SELECT count(*)"
	    ." FROM #__menu"
	    ." WHERE menutype='janews' AND type='component' AND link LIKE 'index.php?option=com_janews&view=section&layout=blog&id=%'";
	  $db->setQuery($query);
	  $num = $db->loadResult();
	  $order = $num ? $num+1 : 1;
	  $component =& JComponentHelper::getComponent('com_janews');
		$query = "INSERT INTO #__menu (menutype,name,alias,link,type,published,parent,
		  componentid,sublevel,ordering,checked_out,checked_out_time,pollid,browserNav,access,utaccess,params)"
	    ." VALUES ('janews','".addslashes($sec->title)."','".addslashes($sec->alias)."','index.php?option=com_janews&view=section&layout=blog&id=".$sec->id."','component','1','0',
	    '".$component->id."','0','".$order."','0','0000-00-00 00:00:00','0','0','0','0','menu_image=-1')";
    $db->setQuery( $query );
    $db->query();
	}
	
  function init() 
  {
    $db = JFactory::getDBO();
    //creat info frontpage
		$query = "SELECT count(*)"
      ." FROM  #__janews_frontpage";
		$db->setQuery( $query );
		$count = $db->loadResult();
		if($count < 1) {
		  $query = "SELECT id FROM #__janews_templates"
        ." GROUP BY type"
        ." ORDER BY type";
		  $db->setQuery( $query );
		  $id_tmpl = $db->loadResultArray();
		  //get ids of published sections
		  $id_sec = $this->getStrListSecIds();
		  //insert new row into frontpage table
		  $query = "INSERT INTO `#__janews_frontpage` (`id`, `title`, `headline`, `blog`, `sections`, `default_f`)
	              VALUES (1, 'frontpage', '".$id_tmpl[1]."', '".$id_tmpl[0]."', '".$id_sec."', '1')";
		  $db->setQuery( $query );
			$db->query();
			$sections = split(',', $id_sec);
			for ($i=0;$i<count($sections);$i++){
			  $query = "SELECT id, title, alias"
			   ." FROM #__sections"
		     ." WHERE id = ".$sections[$i];
	     $db->setQuery($query);
	     $sec = $db->loadObject();
	     $this->creatMenuSection($sec);
			}
		}
		return true;
  }
}

?>